/*
 *  Copyright (C) 2011 Jaime Pavlich-Mariscal
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package cl.ucn.disc.biblio.refcluster.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.xml.bind.PropertyException;

import org.apache.log4j.Logger;

import cl.ucn.disc.biblio.refcluster.control.ReferenceClustererController;
import cl.ucn.disc.biblio.refcluster.util.Command;

/**
 * 
 * @author Jaime Pavlich
 */
public class ReferenceClustererWindow extends javax.swing.JFrame {

	private static final long serialVersionUID = -744476234820635268L;

	/** Creates new form ReferenceMatcherJFrame */
	public ReferenceClustererWindow() {
		controller = new ReferenceClustererController();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					controller.setAuthorClustersFilename(txtAuthorClustersFile.getText());
					controller.setDisambiguateAuthores(cbDisambiguateAuthors.isSelected());
					controller.setBiblioDBDestFolder(txtBiblioDBDestFolder.getText());
					controller.setBiblioDBSourceFolder(txtBiblioDBSourceFolder.getText());
					controller.setReferenceClustersFilename(txtReferenceClustersFilename.getText());
					controller.saveConfig();
				} catch (PropertyException ex) {
					JOptionPane.showMessageDialog(null, ex);
					ex.printStackTrace();
				}
			}
		});
		try {
			initComponents();
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(ReferenceClustererWindow.class.getName()).error("", ex);
		} catch (InstantiationException ex) {
			Logger.getLogger(ReferenceClustererWindow.class.getName()).error("", ex);
		} catch (IllegalAccessException ex) {
			Logger.getLogger(ReferenceClustererWindow.class.getName()).error("", ex);
		} catch (UnsupportedLookAndFeelException ex) {
			Logger.getLogger(ReferenceClustererWindow.class.getName()).error("", ex);
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT
	 * modify this code. The content of this method is always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		try {
			
			folderChooser = new JFileChooser();
			folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

			jLabel1 = new javax.swing.JLabel();
			jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
			txtBiblioDBSourceFolder = new javax.swing.JTextField();
			jButtonBrowseDBFolder = new javax.swing.JButton();
			jLabel2 = new javax.swing.JLabel();
			jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
			txtReferenceClustersFilename = new javax.swing.JTextField();
			jButtonBrowseClusterFile = new javax.swing.JButton();
			jLabel3 = new javax.swing.JLabel();
			jLabel3.setHorizontalAlignment(SwingConstants.LEFT);
			txtBiblioDBDestFolder = new javax.swing.JTextField();
			jButtonBrowseNewDBFolder = new javax.swing.JButton();
			jLabel4 = new javax.swing.JLabel();
			jLabel4.setHorizontalAlignment(SwingConstants.LEFT);
			jLabel5 = new javax.swing.JLabel();
			jLabel5.setHorizontalAlignment(SwingConstants.LEFT);
			jLabel6 = new javax.swing.JLabel();
			jLabel6.setHorizontalAlignment(SwingConstants.LEFT);
			jButtonGenerateClusterFile = new javax.swing.JButton();
			jLabel7 = new javax.swing.JLabel();
			jLabel7.setHorizontalAlignment(SwingConstants.LEFT);
			jLabel8 = new javax.swing.JLabel();
			jLabel8.setHorizontalAlignment(SwingConstants.LEFT);
			jButtonGenerateNewDB = new javax.swing.JButton();

			setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
			setTitle("Reference Clusterer");

			jLabel1.setText("Bibliographic database folder:  ");

			txtBiblioDBSourceFolder.setText(controller.getBiblioDBSourceFolder());

			jButtonBrowseDBFolder.setText("Browse");
			jButtonBrowseDBFolder.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonBrowseDBFolderActionPerformed(evt);
				}
			});

			jLabel2.setText("Cluster file:");

			txtReferenceClustersFilename.setText(controller.getReferenceClustersFilename());

			jButtonBrowseClusterFile.setText("Browse");
			jButtonBrowseClusterFile.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonBrowseClusterFileActionPerformed(evt);
				}
			});

			jLabel3.setText("New bibliographic database folder:");

			txtBiblioDBDestFolder.setText(controller.getBiblioDBDestFolder());

			jButtonBrowseNewDBFolder.setText("Browse");
			jButtonBrowseNewDBFolder.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonBrowseNewDBFolderActionPerformed(evt);
				}
			});

			jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
			jLabel4.setText("Step 1: Select folder of where the bibliographic database is stored");

			jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
			jLabel5.setText("Step 2: Specify the cluster file name");

			jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
			jLabel6.setText("Step 3: Generate the cluster file");

			jButtonGenerateClusterFile.setText("Generate reference cluster file");
			jButtonGenerateClusterFile.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonGenerateClusterFileActionPerformed(evt);
				}
			});

			jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
			jLabel7.setText("Step 5: Select folder for the new database");

			jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
			jLabel8.setText("Step 6: Generate the new database");

			jButtonGenerateNewDB.setText("Generate new database");
			jButtonGenerateNewDB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonGenerateNewDBActionPerformed(evt);
				}
			});

			JLabel lblWarningThisWill = new JLabel("WARNING: This will delete all of the files in the new database folder!");
			lblWarningThisWill.setHorizontalAlignment(SwingConstants.LEFT);
			lblWarningThisWill.setForeground(Color.RED);
			lblWarningThisWill.setFont(new Font("Tahoma", Font.BOLD, 11));

			JLabel lblStepSelect = new JLabel("Step 4: Specify author disambiguation");
			lblStepSelect.setHorizontalAlignment(SwingConstants.LEFT);
			lblStepSelect.setFont(new Font("Tahoma", Font.BOLD, 11));

			boolean disambiguateAuthors = controller.getDisambiguateAuthors();
			lblAuthorClustersFile = new JLabel();
			lblAuthorClustersFile.setHorizontalAlignment(SwingConstants.LEFT);
			lblAuthorClustersFile.setText("Select author cluster file:");
			lblAuthorClustersFile.setEnabled(disambiguateAuthors);

			txtAuthorClustersFile = new JTextField();
			txtAuthorClustersFile.setText(controller.getAuthorClustersFilename());
			txtAuthorClustersFile.setEnabled(disambiguateAuthors);

			buttonBrowseAuthorClusterFile = new JButton();
			buttonBrowseAuthorClusterFile.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					folderChooser.setCurrentDirectory(new File(txtAuthorClustersFile.getText()));
					int returnVal = fileChooser.showOpenDialog(ReferenceClustererWindow.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File clusterFile = fileChooser.getSelectedFile();
						txtAuthorClustersFile.setText(clusterFile.getAbsolutePath());
					}
				}
			});
			buttonBrowseAuthorClusterFile.setText("Browse");
			buttonBrowseAuthorClusterFile.setEnabled(disambiguateAuthors);

			cbDisambiguateAuthors = new JCheckBox("Disambiguate authors?");
			cbDisambiguateAuthors.setSelected(disambiguateAuthors);
			cbDisambiguateAuthors.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					lblAuthorClustersFile.setEnabled(cbDisambiguateAuthors.isSelected());
					txtAuthorClustersFile.setEnabled(cbDisambiguateAuthors.isSelected());
					buttonBrowseAuthorClusterFile.setEnabled(cbDisambiguateAuthors.isSelected());
					btnGenerateAuthorCluster.setEnabled(cbDisambiguateAuthors.isSelected());
				}
			});

			btnGenerateAuthorCluster = new JButton("Generate author cluster file");
			btnGenerateAuthorCluster.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						executeWithProgressBar(new Command() {
							
							@Override
							public void execute() throws Exception {
								controller.createAuthorClusterFileFromWoSFolder(new File(txtBiblioDBSourceFolder.getText()), new File(txtAuthorClustersFile.getText()));
								
							}
						});					
						
					} catch (Throwable t) {
						JOptionPane.showMessageDialog(null, t);
						t.printStackTrace();
					}
				}
			});
			btnGenerateAuthorCluster.setEnabled(disambiguateAuthors);

			scrollPane = new JScrollPane();

			javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
			layout.setHorizontalGroup(layout
					.createParallelGroup(Alignment.TRAILING)
					.addGroup(
							Alignment.LEADING,
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(Alignment.LEADING)
													.addGroup(
															layout.createSequentialGroup()
																	.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE).addContainerGap())
													.addGroup(
															layout.createSequentialGroup()
																	.addGroup(
																			layout.createParallelGroup(Alignment.LEADING)
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(jButtonGenerateClusterFile)
																									.addPreferredGap(ComponentPlacement.RELATED, 420,
																											Short.MAX_VALUE))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(lblStepSelect, GroupLayout.DEFAULT_SIZE,
																											GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																									.addGap(414))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(cbDisambiguateAuthors,
																											GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
																											Short.MAX_VALUE).addGap(471))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(jLabel4, GroupLayout.DEFAULT_SIZE,
																											GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																									.addGap(258))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(btnGenerateAuthorCluster)
																									.addPreferredGap(ComponentPlacement.RELATED, 439,
																											Short.MAX_VALUE))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(lblAuthorClustersFile,
																											GroupLayout.PREFERRED_SIZE, 217,
																											GroupLayout.PREFERRED_SIZE)
																									.addPreferredGap(ComponentPlacement.RELATED)
																									.addComponent(txtAuthorClustersFile,
																											GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
																									.addPreferredGap(ComponentPlacement.RELATED)
																									.addComponent(buttonBrowseAuthorClusterFile))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, 217,
																											GroupLayout.PREFERRED_SIZE)
																									.addPreferredGap(ComponentPlacement.RELATED)
																									.addComponent(txtBiblioDBDestFolder,
																											GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
																									.addGap(10).addComponent(jButtonBrowseNewDBFolder))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(jButtonGenerateNewDB)
																									.addPreferredGap(ComponentPlacement.RELATED, 459,
																											Short.MAX_VALUE))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(lblWarningThisWill, GroupLayout.DEFAULT_SIZE,
																											GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																									.addGap(251))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(jLabel8, GroupLayout.DEFAULT_SIZE,
																											GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																									.addGap(429))
																					.addGroup(
																							Alignment.TRAILING,
																							layout.createSequentialGroup()
																									.addComponent(jLabel7, GroupLayout.DEFAULT_SIZE,
																											GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																									.addGap(392))
																					.addGroup(
																							layout.createSequentialGroup()
																									.addGroup(
																											layout.createParallelGroup(Alignment.LEADING)
																													.addGroup(
																															layout.createSequentialGroup()
																																	.addComponent(
																																			jLabel2,
																																			GroupLayout.PREFERRED_SIZE,
																																			216,
																																			GroupLayout.PREFERRED_SIZE)
																																	.addPreferredGap(
																																			ComponentPlacement.RELATED))
																													.addGroup(
																															layout.createSequentialGroup()
																																	.addGroup(
																																			layout.createParallelGroup(
																																					Alignment.LEADING)
																																					.addComponent(
																																							jLabel1,
																																							GroupLayout.DEFAULT_SIZE,
																																							202,
																																							Short.MAX_VALUE)
																																					.addComponent(
																																							jLabel5,
																																							Alignment.TRAILING,
																																							GroupLayout.DEFAULT_SIZE,
																																							GroupLayout.DEFAULT_SIZE,
																																							Short.MAX_VALUE))
																																	.addGap(26)))
																									.addPreferredGap(ComponentPlacement.RELATED)
																									.addGroup(
																											layout.createParallelGroup(Alignment.LEADING)
																													.addGroup(
																															Alignment.TRAILING,
																															layout.createSequentialGroup()
																																	.addComponent(
																																			txtBiblioDBSourceFolder,
																																			GroupLayout.DEFAULT_SIZE,
																																			538,
																																			Short.MAX_VALUE)
																																	.addPreferredGap(
																																			ComponentPlacement.UNRELATED)
																																	.addComponent(
																																			jButtonBrowseDBFolder))
																													.addGroup(
																															Alignment.TRAILING,
																															layout.createSequentialGroup()
																																	.addComponent(
																																			txtReferenceClustersFilename,
																																			GroupLayout.DEFAULT_SIZE,
																																			310,
																																			Short.MAX_VALUE)
																																	.addPreferredGap(
																																			ComponentPlacement.UNRELATED)
																																	.addComponent(
																																			jButtonBrowseClusterFile)))))
																	.addContainerGap())
													.addGroup(
															layout.createSequentialGroup()
																	.addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																	.addGap(461)))));
			layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(
					layout.createSequentialGroup()
							.addContainerGap()
							.addComponent(jLabel4)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(
									layout.createParallelGroup(Alignment.BASELINE)
											.addComponent(jButtonBrowseDBFolder)
											.addComponent(txtBiblioDBSourceFolder, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
													GroupLayout.PREFERRED_SIZE).addComponent(jLabel1))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(jLabel5)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(
									layout.createParallelGroup(Alignment.BASELINE)
											.addComponent(jLabel2)
											.addComponent(txtReferenceClustersFilename, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
													GroupLayout.PREFERRED_SIZE).addComponent(jButtonBrowseClusterFile))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(jLabel6)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(jButtonGenerateClusterFile)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblStepSelect)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cbDisambiguateAuthors)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnGenerateAuthorCluster)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(
									layout.createParallelGroup(Alignment.LEADING)
											.addComponent(lblAuthorClustersFile)
											.addGroup(
													layout.createParallelGroup(Alignment.BASELINE)
															.addComponent(txtAuthorClustersFile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
																	GroupLayout.PREFERRED_SIZE).addComponent(buttonBrowseAuthorClusterFile)))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(jLabel7)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(
									layout.createParallelGroup(Alignment.BASELINE)
											.addComponent(jLabel3)
											.addComponent(txtBiblioDBDestFolder, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
													GroupLayout.PREFERRED_SIZE).addComponent(jButtonBrowseNewDBFolder))
							.addPreferredGap(ComponentPlacement.RELATED).addComponent(jLabel8).addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblWarningThisWill).addPreferredGap(ComponentPlacement.RELATED).addComponent(jButtonGenerateNewDB)
							.addPreferredGap(ComponentPlacement.RELATED).addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
							.addContainerGap()));

			textAreaLog = new JTextArea();
			scrollPane.setViewportView(textAreaLog);
			textAreaLog.setWrapStyleWord(true);
			TextAreaAppender.setTextArea(textAreaLog);
			getContentPane().setLayout(layout);

			java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			setBounds((screenSize.width - 668) / 2, (screenSize.height - 368) / 2, 770, 596);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e);
			System.exit(1);

		}
	}// </editor-fold>//GEN-END:initComponents

	private void jButtonBrowseDBFolderActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonBrowseDBFolderActionPerformed
		folderChooser.setCurrentDirectory(new File(txtBiblioDBSourceFolder.getText()));
		int returnVal = folderChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File wosDBFolder = folderChooser.getSelectedFile();
			txtBiblioDBSourceFolder.setText(wosDBFolder.getAbsolutePath());
		}
	}// GEN-LAST:event_jButtonBrowseDBFolderActionPerformed

	private void jButtonGenerateClusterFileActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonGenerateClusterFileActionPerformed
		try {
			if (!txtBiblioDBSourceFolder.getText().isEmpty() && !txtReferenceClustersFilename.getText().isEmpty()) {
				final File wosDBFolder = new File(txtBiblioDBSourceFolder.getText());
				final File clusterFile = new File(txtReferenceClustersFilename.getText());
				
				executeWithProgressBar( new Command() {
					@Override
					public void execute() throws Exception {
						controller.createReferenceClusterFileFromWoSFolder(wosDBFolder, clusterFile);
					}
				});
				
				
			} else {
				JOptionPane.showMessageDialog(this, "Select Bibliographic database and cluster file", "Error", JOptionPane.ERROR_MESSAGE);
			}

		} catch (Throwable t) {
			JOptionPane.showMessageDialog(this, t.toString(), "Error", JOptionPane.ERROR_MESSAGE);
			t.printStackTrace();
		}
	}// GEN-LAST:event_jButtonGenerateClusterFileActionPerformed

	public void executeWithProgressBar(final Command command) {
		new SwingWorker<Object, Object>() {

			@Override
			protected Object doInBackground() throws Exception {
				command.execute();
				return null;
			}

			@Override
			protected void done() {
				
			}
			
		}.execute();
	}

	private void jButtonBrowseClusterFileActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonBrowseClusterFileActionPerformed
		fileChooser.setCurrentDirectory(new File(txtReferenceClustersFilename.getText()));
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File clusterFile = fileChooser.getSelectedFile();
			txtReferenceClustersFilename.setText(clusterFile.getAbsolutePath());
		}
	}// GEN-LAST:event_jButtonBrowseClusterFileActionPerformed

	private void jButtonGenerateNewDBActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonGenerateNewDBActionPerformed
		try {
			final File wosDBFolder = new File(txtBiblioDBSourceFolder.getText());
			final File clusterFile = new File(txtReferenceClustersFilename.getText());
			final File newDBFolder = new File(txtBiblioDBDestFolder.getText());
			final File authorClusterFile;
			if (cbDisambiguateAuthors.isSelected()) {
				authorClusterFile = new File(txtAuthorClustersFile.getText());
			} else {
				authorClusterFile = null;
			}
			
			executeWithProgressBar(new Command() {
				
				@Override
				public void execute() throws Exception {
					controller.replaceReferencesAndAuthorsOfFolder(wosDBFolder, newDBFolder, clusterFile, authorClusterFile);
					
				}
			});
					


			

		} catch (Throwable t) {
			JOptionPane.showMessageDialog(this, t.toString(), "Error", JOptionPane.ERROR_MESSAGE);
			t.printStackTrace();
		}
	}// GEN-LAST:event_jButtonGenerateNewDBActionPerformed

	private void jButtonBrowseNewDBFolderActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonBrowseNewDBFolderActionPerformed
		folderChooser.setCurrentDirectory(new File(txtBiblioDBDestFolder.getText()));
		int returnVal = folderChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File newDBFolder = folderChooser.getSelectedFile();
			txtBiblioDBDestFolder.setText(newDBFolder.getAbsolutePath());
		}
	}// GEN-LAST:event_jButtonBrowseNewDBFolderActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new ReferenceClustererWindow().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButtonBrowseClusterFile;
	private javax.swing.JButton jButtonBrowseDBFolder;
	private javax.swing.JButton jButtonBrowseNewDBFolder;
	private javax.swing.JButton jButtonGenerateClusterFile;
	private javax.swing.JButton jButtonGenerateNewDB;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JTextField txtBiblioDBSourceFolder;
	private javax.swing.JTextField txtReferenceClustersFilename;
	private javax.swing.JTextField txtBiblioDBDestFolder;
	private JLabel lblAuthorClustersFile;
	private JTextField txtAuthorClustersFile;
	private JButton buttonBrowseAuthorClusterFile;
	private JCheckBox cbDisambiguateAuthors;
	private JButton btnGenerateAuthorCluster;

	private final ReferenceClustererController controller;
	private JFileChooser fileChooser = new JFileChooser();
	private JFileChooser folderChooser;
	private JTextArea textAreaLog;
	private JScrollPane scrollPane;
}
